import java.net.DatagramPacket;
import java.net.DatagramSocket;


public class UDPCoreServer extends Thread{

	public static final int MaxRequests = 12;

	private DatagramSocket serverSocketClient;
	private DatagramSocket serverSocketPrinter;
	private int portC;
	private int portP;
	
	private DatagramPacket receivedPacket;
	
	public UDPCoreServer(int portClient, int portPrinter) {
		
		this.portC = portClient;
		this.portP = portPrinter;
	}
	
	@Override
	public void run()
	{
		try {
			
			this.serverSocketClient = new DatagramSocket(portC);
			this.serverSocketPrinter = new DatagramSocket(portP);
		} catch (Exception e) {
			
			System.err.println("Exceptie la crearea DatagramSocket-ului: " + e);
			e.printStackTrace();
			return;
		}
		
		for (int i = 0; i < MaxRequests; i++) 
		{
			System.out.println("[UDPServer] Waiting for a new request");
			
			try {
				
				byte[] buf = new byte[1024];
				receivedPacket = new DatagramPacket(buf, buf.length);
				this.serverSocketClient.receive(receivedPacket);				
			} catch (Exception e) {
			
				System.err.println("Eroare la receive: " + e);
				e.printStackTrace();
			}
			
			try {
				
				byte[] buf = new byte[1024];
				serverSocketPrinter.send(receivedPacket);
				
			} catch (Exception e) {}
		}		
		
		try {
			
			this.serverSocketClient.close();
			this.serverSocketPrinter.close();
		} catch (Exception e) {
			
			System.err.println("DatagramSocket close exception: " + e);
			e.printStackTrace();
		}
	}
}
